const app = getApp(),url = app.globalData.url;
import util from '../../utils/util'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    curNav: '',
    navTypes:[
      {label: '全部简历', type: ''},
      {label: '邀面试', type: 4},
      {label: '感兴趣', type: 2},
      {label: '不感兴趣', type: 3},
    ],
    jlList:[],
    isLoading: false,
    search:{
      ageBegin: "",
      ageEnd: "",
      applyState: "",
      createTime: "",
      educational: "",
      experience: "",
      positionName: "",
      currentPage: 1,
      pageSize: 10,
      enterpriseUid: "",
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad (options) {
    let userInfo = wx.getStorageSync('userInfo')
    this.data.search.enterpriseUid = userInfo.enterpriseUid
    this.data.search.applyState = options.jltype
    this.setData({
      search: this.data.search,
      curNav: options.jltype
    })
    this.getjlList()
  },
  reload() {
    let filterForm = wx.getStorageSync('jl_search')
    filterForm.currentPage = 1
    this.setData({
      jlList: [],
      curNav: filterForm.applyState,
      search: Object.assign({}, this.data.search, filterForm)
    })
    this.getjlList()
  },
  onNavChange(e){
    let {id, type} = e.currentTarget.dataset
    this.data.search.applyState = type
    this.data.search.currentPage = 1
    this.setData({
      curNav: type,
      search: this.data.search,
      jlList:[]
    })
    this.getjlList()
  },
  getjlList(){
    let _this = this
    this.setData({ isLoading: true })
    wx.showLoading()
    wx.request({
      url: url + 'applyCVManagement/getAllApplyCVByPage',
      header:{ 'Content-Type': 'application/json',},
      data: this.data.search,
      method: "POST",
      success(res){
        let resData = res.data
        if(resData.code != 200 || !resData.data){
          wx.showToast({
            title: res.data.msg,
            icon:'error'
          })
          return
        }
        let jlList = (resData.data.content || []).filter(item => item.id)
        if(_this.data.search.currentPage > 1 && jlList.length === 0){
          wx.showToast({
            title: '到底了~~',
            icon:'none'
          })
          return
        }
        _this.setData({
          jlList: [..._this.data.jlList, ...jlList]
        })
      },
      complete(){
        wx.hideLoading()
        _this.setData({ isLoading: false})
      }
    })
  },
  toFilter(){
    wx.navigateTo({
      url: '/Company/jlfilter/jlfilter',
    })
  },
  toChat(e){
    let {index, item} = e.currentTarget.dataset;
    this.data.jlList[index].laiYuan = 0
    setTimeout(() => {
      this.setData({
        jlList: this.data.jlList
      })
    }, 1000);
    wx.navigateTo({
      url: '/pages/chat/chat?position='+ JSON.stringify(item),
    })
  },
  toJLDetail(e){
    let {item } = e.currentTarget.dataset
    if(item.applyState != 5){
      wx.navigateTo({
        url: `/HomePage/linejianli/linejianli?type=view&uid=${item.userUid}&pid=${item.id}`
      })
      return
    }
    this.updateApplyState(1, item.userUid, item.id)
  },
  updateApplyState(state, uid, pid){
    let params = {
      applyPositionId: pid, 
      applyState: state // 1查看 2感兴趣 3不感兴趣 4邀面试 5待查阅
    }
    wx.request({
      url: url+ 'applyCVManagement/getUpdatePositionApplyState',
      method: "POST",
      header:{ 'Content-Type': 'application/json',},
      data: params,
      success(res){
        if(res.data.code == 200){
          wx.navigateTo({
            url: `/HomePage/linejianli/linejianli?type=view&uid=${uid}&pid=${pid}`
          })
          return
        }
        wx.showToast({
          title: res.data.msg || "操作失败",
          icon: 'none'
        })
      },
      fail(err){
        wx.showToast({
          title: "操作失败",
          icon: 'none'
        })
      }
    })
  },
  
  onReachBottom: function () {
    this.data.search.currentPage++
    this.setData({
      search: this.data.search
    })
    this.getjlList()
  },
  
  onHide: function () {
    this.data.search.currentPage = 0
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.data.search.currentPage = 0
  },
})